package cn.baiweigang.uu.task;

import cn.baiweigang.uu.enums.StrategyEnum;
import cn.baiweigang.uu.service.analyse.AnalyseService;
import cn.baiweigang.uu.service.calculate.CalculateService;
import cn.baiweigang.uu.service.fetch.EastmoneyService;
import cn.baiweigang.uu.service.fetch.MaiRuiService;
import cn.baiweigang.uu.service.fetch.SohuService;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@Configuration
@EnableScheduling
@EnableAsync
public class EveryDayTask {

    @Autowired
    private MaiRuiService maiRuiService;

    @Autowired
    private SohuService sohuService;

    @Autowired
    private CalculateService calculateService;

    @Autowired
    private AnalyseService  analyseService;

    @Autowired
    private EastmoneyService eastmoneyService;

    //每天16:05 拉取最新股票列表
    @Scheduled(cron = "00 05 16 * * ?")
    private void stockList() {
        try {
            LogUtil.logInfo("拉取最新股票列表");
            maiRuiService.pullStockBaseInfo();
        } catch (Throwable e) {
            LogUtil.logError("拉取最新股票列表 异常 " + e.getMessage());
        }
    }

    //每天19:05 拉取当天的历史数据
    @Scheduled(cron = "00 05 20 * * ?")
    private void dayHistorSohu() {
        try {
            String ds=DateUtil.getDs();
            LogUtil.logInfo("拉取当天的历史数据");
            //拉取中国市场的股票 日K线历史数据
            sohuService.StockHistoryDayForCN(ds,ds);
        } catch (Throwable e) {
            LogUtil.logError("拉取当天的历史数据 异常 " + e.getMessage());
        }
    }

    //每天20:15 获取东方财富排名
    @Scheduled(cron = "00 15 20 * * ?")
    private void rank() {
        try {
            String ds = DateUtil.getDs();
            LogUtil.logInfo(" 获取东方财富排名 开始 ds=" + ds);
            eastmoneyService.updateRank(ds);
        } catch (Throwable e) {
            LogUtil.logError(" 获取东方财富排名 异常 " + e.getMessage());
        }
    }

    //每天21:05 对当天的日线数据进行计算
    @Scheduled(cron = "00 05 21 * * ?")
    private void calc() {
        try {
            String ds = DateUtil.getDs();
            LogUtil.logInfo("当天日线数据计算 开始 ds=" + ds);
            calculateService.calc(ds);
        } catch (Throwable e) {
            LogUtil.logError("当天日线数据计算 异常 " + e.getMessage());
        }
    }


    //每天21:30 执行选股策略
    @Scheduled(cron = "00 30 21 * * ?")
    private void analyse() {
        try {
            String ds = DateUtil.getDs();
            LogUtil.logInfo("执行选股策略 开始 ds=" + ds);
            analyseService.analyse(ds);

            LogUtil.logInfo("刷新策略选股排名");
            String before = DateUtil.getAddDays(DateUtil.getDs(), -7);
            analyseService.updatePM(before, StrategyEnum.MA_3P);
            analyseService.updatePM(before, StrategyEnum.CHANGE_PM);

        } catch (Throwable e) {
            LogUtil.logError("执行选股策略 异常 " + e.getMessage());
        }
    }
}
